package com.zjdiepu.www.biz.system;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.system.BaseOrganizationMapper;
import com.zjdiepu.www.ibiz.system.IBaseOrganizationService;
import com.zjdiepu.www.model.system.dto.BaseOrganization;
import com.zjdiepu.www.model.system.ext.BaseOrganizationExt;

/**
 * 行政机构Service层操作实现<br/>
 * @author winby<br/>
 * @time 2017年04月27日 17时03分<br/>
 * @since 1.0<br/>
 */
@Service
public class BaseOrganizationServiceImpl extends BaseService implements IBaseOrganizationService {
	public static final Logger logger = LoggerFactory.getLogger(BaseOrganizationServiceImpl.class);

	@Autowired
	private BaseOrganizationMapper baseOrganizationMapper;
	
	@Override
	public void saveUpdateAndDelete(BaseOrganization baseOrganization, OperateType operType) {
		switch (operType) {
		case add:
			baseOrganization.initInsertData(baseOrganization.getModifier(), baseOrganization.getModifierName());
			baseOrganizationMapper.insert(baseOrganization);
			break;
		case edit:
			baseOrganization.initUpdateData(baseOrganization.getModifier(), baseOrganization.getModifierName());
			// 影响的记录数
			int updateCount = baseOrganizationMapper.updateByPrimaryKeySelective(baseOrganization);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			baseOrganization.initDeleteData(baseOrganization.getModifier(), baseOrganization.getModifierName());
			// 影响的记录数
			int deleteCount = baseOrganizationMapper.updateByPrimaryKeySelective(baseOrganization);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<BaseOrganization> selectByExt(BaseOrganizationExt baseOrganizationExt) {
		return baseOrganizationMapper.selectByExt(baseOrganizationExt);
	}
	
	@Override
	public int countByExt(BaseOrganizationExt baseOrganizationExt){
		return baseOrganizationMapper.countByExt(baseOrganizationExt);
	}
	
	@Override
	public BaseOrganization selectById(String id) {
		return baseOrganizationMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			baseOrganizationMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(BaseOrganization baseOrganization) {
		return baseOrganizationMapper.countRefined(baseOrganization);
	}

	@Override
	public BaseOrganization selectRefined(BaseOrganization baseOrganization) {
		return baseOrganizationMapper.selectRefined(baseOrganization);
	}

	@Override
	public boolean repeat(BaseOrganization baseOrganization) {
		int count = countRefined(baseOrganization);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(baseOrganization.getId())) {
			return true;
		}
		BaseOrganization dbItem = selectRefined(baseOrganization);
		if(null == dbItem) {
			return false;
		}
		return !baseOrganization.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return baseOrganizationMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
}
